package com.chenwut.studentManagerSystem;

import com.chenwut.studentManagerSystem.Tool.verificationCode;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class App {
    static ArrayList<Users> usersList = new ArrayList<>();
    static {
        usersList.add(new Users("lisi","1234qwer","111111222233332222","13312345678"));
    }

    public static void main(String[] args) {
        while (true) {
            System.out.println("------------欢迎来到学生管理系统------------");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.忘记密码");
            System.out.println("4.退出");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1" -> login(usersList);
                case "2" -> register(usersList);
                case "3" -> System.out.println("3，忘记密码");
                case "4" -> {
                    System.out.println("退出");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    //登录
    public static void login(ArrayList<Users> usersList) {
        for (int i = 0; i < usersList.size(); i++) {
            System.out.println(usersList.get(i).getUserName() + "\t" + usersList.get(i).getUserPassword() + "\t"
                    + usersList.get(i).getUserId() + "\t" + usersList.get(i).getUserPhoneNum());
        }
        //1.键盘录入用户名
        Scanner sc = new Scanner(System.in);
        int Index;
        String userName;
        for (int i = 0; i < 3; i++) {
            while (true) {
                System.out.println("请输入用户名：");
                userName = sc.next();
                boolean flag1 = checkUserName(userName);
                if (!flag1) {
                    System.out.println("用户名不符合要求");
                    continue;
                }
                boolean flag2 = contains(usersList, userName);
                if (flag2) {
                    Index = returnIndex(usersList, userName);
                    break;
                } else {
                    System.out.println("用户名不存在，重新输入");
                    continue;
                }
            }
            //2.键盘录入密码
            System.out.println("请输入密码：");
            String Pwd = sc.next();

            //2.键盘录入验证码
            while (true) {
                String code = verificationCode.getCode();
                System.out.println("当前验证码为：" + code);
                System.out.println("请输入验证码：");
                String verificationCode = sc.next();
                if (!verificationCode.equalsIgnoreCase(code)) {
                    System.out.println("验证码不正确");
                    continue;
                } else {
                    //StudentSystem.main();
                    System.out.println("验证码正确");
                    break;
                }
            }
            Users userinfo = new Users(userName, Pwd, null, null);
            boolean loginresult = checkInfo(usersList, userinfo);
            if (loginresult) {
                System.out.println("登录成功，可以使用学生管理系统了");
                StudentSystem.startStudentSystem();
                break;
            } else {
                System.out.println("登陆失败，账号或密码错误");
                if (i == 2) {
                    System.out.println("当前账号" + userName + "被锁定，请联系客服：xxx-xxxx");
                    return;
                } else {
                    System.out.println("用户名或密码错误，还剩下" + (2 - i) + "次机会");
                }
            }
        }

    }

    private static boolean checkInfo(ArrayList<Users> usersList, Users userinfo) {
        for (int i = 0; i < usersList.size(); i++) {
            Users user = usersList.get(i);
            if (user.getUserName().equals(userinfo.getUserName()) &&
                    user.getUserPassword().equals(userinfo.getUserPassword())) {
                return true;
            }
        }
        return false;
    }

    //注册
    public static void register(ArrayList<Users> usersList) {
        Scanner sc = new Scanner(System.in);
        Users user = new Users();

        //1.键盘录入用户名
        while (true) {
            System.out.println("请输入用户名：");
            String userName = sc.next();
            boolean flag1 = checkUserName(userName);
            if (!flag1) {
                System.out.println("用户名不符合要求");
                continue;
            }
            boolean flag2 = contains(usersList, userName);
            if (flag2) {
                System.out.println("用户名已存在");
                continue;
            } else {
                user.setUserName(userName);
                break;
            }
        }

        while (true) {
            //2.键盘录入密码
            System.out.println("请输入密码：");
            String Pwd = sc.next();
            System.out.println("请再次输入密码：");
            String againPwd = sc.next();
            if (!Pwd.equals(againPwd)) {
                System.out.println("两次密码不一致，重新录入");
                continue;
            } else {
                System.out.println("两次密码一致，继续录入其他信息");
                user.setUserPassword(Pwd);
                break;
            }
        }

        while (true) {
            System.out.println("请输入身份证号码：");
            String userId = sc.next();

            boolean flag3 = checkID(userId);
            if (flag3) {
                System.out.println("格式正确");
                user.setUserId(userId);
                break;
            } else {
                System.out.println("格式错误，请重新输入");
                continue;
            }
        }

        while (true) {
            System.out.println("请输入手机号：");
            String PhoneNum = sc.next();

            boolean flag4 = checkPhoneNum(PhoneNum);
            if (flag4) {
                System.out.println("电话号码符合要求");
                user.setUserPhoneNum(PhoneNum);
                break;
            } else {
                System.out.println("电话号码不符合要求,重新输入");
                continue;
            }
        }
        usersList.add(user);
    }


    //忘记密码
    public static void forgetPassword(ArrayList<Users> usersList) {
        //1.键盘录入用户名
        Scanner sc = new Scanner(System.in);
        int Index;
        String userName;
        System.out.println("请输入用户名：");
        userName = sc.next();
        boolean flag2 = contains(usersList, userName);
        if (!flag2) {
            System.out.println("用户名不存在，请先注册");
            return;
        }
        //键盘录入身份证号码和手机号码
        System.out.println("请输入身份证号码");
        String personID = sc.next();
        System.out.println("请输入手机号码");
        String phoneNum = sc.next();

        int index = returnIndex(usersList, userName);
        Users user = usersList.get(index);
        //比较手机号码和身份证号码
        if (!(user.getUserId().equalsIgnoreCase(personID) && user.getUserPhoneNum().equals(phoneNum))) {
            System.out.println("身份证号码或手机号码输入有误，不能修改密码");
            return;
        }

        String password;
        while (true) {
            System.out.println("请输入新的密码");
            password = sc.next();
            System.out.println("请再次输入新的密码");
            String againpassword = sc.next();
            if (password.equals(againpassword)) {
                System.out.println("两次密码输入一致");
                break;
            } else {
                System.out.println("两次密码输入不一致，请重新输入");
                continue;
            }
        }
        user.setUserPassword(password);
        System.out.println("密码修改成功");
    }

    public static boolean checkUserName(String username) {
        String pattern = "^(?![0-9]{3,15}$)[a-zA-Z0-9]{3,15}$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(username);
        if (m.find()) {
            return true;
        } else {
            //System.out.println("用户名不符合要求");
            return false;
        }
    }

    public static boolean contains(ArrayList<Users> usersList, String Name) {
        int result = returnIndex(usersList, Name);
        if (result >= 0) {
            return true;
        } else {
            return false;
        }
    }

    public static int returnIndex(ArrayList<Users> usersList, String Name) {
        String userName;
        for (int i = 0; i < usersList.size(); i++) {
            userName = usersList.get(i).getUserName();
            if (Name.equals(userName)) {
                return i;
            }
        }
        return -1;
    }


    private static boolean checkID(String id) {
        //必须18位
        if (id.length() != 18) {
            return false;
        }
        //不能以0开头
        if (id.startsWith("0")) {
            return false;
        }
        //前17位必须为数字
        for (int i = 0; i < id.length() - 1; i++) {
            char c = id.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        //最后一位为数字或者X或x
        char endchar = id.charAt(id.length() - 1);
        if ((endchar >= '0' && endchar <= '9') || (endchar == 'X') || (endchar == 'x')) {
            return true;
        } else {
            return false;
        }
    }

    private static boolean checkPhoneNum(String phoneNum) {
        String patternString = "^[1-9][0-9]{10}$";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(phoneNum);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

}
